/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.hobbiestag.process;

import com.apt.tag.Connect_DB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bach
 */
public class Creates_Hobbies {

    public static boolean createHobbies(String likingname, String liking_Model_name) {

//;select @@IDENTITY as Liking_Models_id
        boolean re = false;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = Connect_DB.getConnection();
            String sql="INSERT INTO [Matrimony].[dbo].[Liking_Cates]\n"
                        + "           ([Liking_Status]\n"
                        + "           ,[Liking_Name])\n"
                        + "     VALUES(1,?)";
            con.setAutoCommit(false);// transaction
            ps=con.prepareStatement(sql);
            ps.setString(1, likingname);
            String qurey = "SELECT [Liking_Status]\n"
                    + "      ,[Liking_Name]\n"
                    + "      ,[Liking_Id]\n"
                    + "  FROM [Matrimony].[dbo].[Liking_Cates]"
                    + "  where [Liking_Name]=?\n";
            if (checkusername(qurey, likingname, con)) {
                int i = ps.executeUpdate("INSERT INTO [Matrimony].[dbo].[Liking_Cates]\n"
                        + "           ([Liking_Status]\n"
                        + "           ,[Liking_Name])\n"
                        + "     VALUES(1,'" + likingname + "') ", ps.RETURN_GENERATED_KEYS);
                if (i > 0) {

                    rs = ps.getGeneratedKeys();
                    rs.next();
                    int lik_model_id = rs.getInt(1);
                    int likId = create_Liking(liking_Model_name, lik_model_id, con);
                    if (likId > 0) {
                        re = true;
                    }
                }
            }
            con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                Logger.getLogger(Creates_Hobbies.class.getName()).log(Level.SEVERE, null, ex);
            }
        } finally {
            Connect_DB.close(null, null, ps, con);
        }
        return re;
    }

    public static int create_Liking(String liking_name, int liking_id, Connection con) {

        int re = 0;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {

            String qurey = "SELECT  [Liking_Model_Id]\n"
                    + "      ,[Liking_Id]\n"
                    + "      ,[Liking_Model_Name]\n"
                    + "      ,[Liking_Model_Status]\n"
                    + "  FROM [Matrimony].[dbo].[Liking_Models]"
                    + "  where [Liking_Model_Name]=?\n";
            String sql="INSERT INTO [Matrimony].[dbo].[Liking_Models]\n"
                        + "           ([Liking_Id],[Liking_Model_Name]\n"
                        + "           ,[Liking_Model_Status])\n"
                        + "     VALUES(?,?,1)";
            ps=con.prepareStatement(sql);
            ps.setInt(1, liking_id);
            ps.setString(2, liking_name);
            if (checkusername(qurey, liking_name, con)) {
                int i = ps.executeUpdate("INSERT INTO [Matrimony].[dbo].[Liking_Models]\n"
                        + "           ([Liking_Id],[Liking_Model_Name]\n"
                        + "           ,[Liking_Model_Status])\n"
                        + "     VALUES(" + liking_id + ",'" + liking_name + "',1)", ps.RETURN_GENERATED_KEYS);
                if (i > 0) {
                    rs = ps.getGeneratedKeys();
                    rs.next();
                    re = rs.getInt(1);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }

    public static boolean checkusername(String qurey, String name, Connection cnn) {
        boolean re = true;
        PreparedStatement ps = null;

        ResultSet rs = null;
        try {
            ps = cnn.prepareStatement(qurey);
            ps.setString(1, name);
            rs = ps.executeQuery();
            if (rs.next()) {

                re = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }
}
//
//
//
//public static boolean update_Hobbies(int liking_id, int liking_Model_Id, Connection con) {
//        String up = "UPDATE [Matrimony].[dbo].[Liking_Models]\n"
//                + "   SET [Liking_Id] = ?\n"
//                + " WHERE Liking_Model_Id=?";
//
////        UPDATE [Matrimony].[dbo].[Liking_Models]
////   SET [Liking_Id] = 1
////      
//// WHERE Liking_Model_Id=193
//        boolean re = false;
//        PreparedStatement ps = null;
//        try {
//
//            ps = con.prepareStatement(up);
//            ps.setInt(1, liking_id);
//            ps.setInt(2, liking_Model_Id);
//
//            int i = ps.executeUpdate();
//            if (i > 0) {
//                re = true;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return re;
//    }
//int i = ps.executeUpdate("INSERT INTO [Matrimony].[dbo].[Liking_Models]\n"
//                + "           ([Liking_Model_Name]\n"
//                + "           ,[Liking_Model_Status])\n"
//                + "     VALUES("+liking_Model_name+",1) ", ps.RETURN_GENERATED_KEYS);
//
//String up = "INSERT INTO [Matrimony].[dbo].[Liking_Models]\n"
//                + "           ([Liking_Model_Name]\n"
//                + "           ,[Liking_Model_Status])\n"
//                + "     VALUES(?,1);select @@IDENTITY as Liking_Models_id ";